﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MRT_App
{
	public partial class Form2 : Form
	{
        private DotNetOpenSource.Controls.ImageMap imageMap1;
        private System.ComponentModel.Container components = null;
        private Label label3;
        private ComboBox fromBox;
        private PictureBox pictureBox1;
        private Label label2;
        private Label label1;
        private ComboBox toBox;
        private Label label4;
        private Button btnClear;
        private Label label5;

        private String startStn = null;
        private String endStn = null;
        private Panel panel1;
        private Label label6;
        private TextBox txtRoute;
        private ArrayList pathIndex = null;

        private LogicServer ls;

		public Form2()
		{
            InitializeComponent();
            this.pathIndex = new ArrayList();
            AddStation();

            // create an instance of LogicServer
            ls = new LogicServer();
            ls.Init(""); // Must do this!!!
            ls.Load("mrt.xpl"); // Load the compiled mrt.xpl
		}

        private void AddStation()
        {
            //East-West Line
            this.imageMap1.AddElipse("Joo Koon", 37, 277, 10);
            this.imageMap1.AddElipse("Pioneer", 62, 277, 10);
            this.imageMap1.AddElipse("Boon Lay", 87, 277, 10);
            this.imageMap1.AddElipse("Lakeside", 112, 277, 10);
            this.imageMap1.AddElipse("Chinese Garden", 136, 277, 10);
            this.imageMap1.AddElipse("Jurong East", 166, 277, 10);
            this.imageMap1.AddElipse("Clementi", 196, 301, 10);
            this.imageMap1.AddElipse("Dover", 216, 322, 10);
            this.imageMap1.AddElipse("Buona Vista", 237, 343, 10);
            this.imageMap1.AddElipse("Commonwealth", 259, 364, 10);
            this.imageMap1.AddElipse("Queenstown", 278, 382, 10);
            this.imageMap1.AddElipse("Redhill", 293, 397, 10);
            this.imageMap1.AddElipse("Tiong Bahru", 309, 414, 10);
            this.imageMap1.AddElipse("Outram Park", 330, 436, 10);
            this.imageMap1.AddElipse("Tanjong Pagar", 355, 460, 10);
            this.imageMap1.AddElipse("Raffles Place", 404, 457, 10);
            this.imageMap1.AddElipse("City Hall", 425, 437, 10);
            this.imageMap1.AddElipse("Bugis", 471, 387, 10);
            this.imageMap1.AddElipse("Lavender", 491, 366, 10);
            this.imageMap1.AddElipse("Kallang", 513, 344, 10);
            this.imageMap1.AddElipse("Aljunied", 540, 327, 10);
            this.imageMap1.AddElipse("Paya Lebar", 563, 328, 10);
            this.imageMap1.AddElipse("Eunos", 604, 327, 10);
            this.imageMap1.AddElipse("Kembangan", 633, 327, 10);
            this.imageMap1.AddElipse("Bedok", 660, 327, 10);
            this.imageMap1.AddElipse("Tanah Merah", 689, 326, 10);
            this.imageMap1.AddElipse("Simei", 729, 307, 10);
            this.imageMap1.AddElipse("Tampines", 749, 286, 10);
            this.imageMap1.AddElipse("Pasir Ris", 770, 265, 10);

            this.imageMap1.AddElipse("Expo", 719, 369, 10);
            this.imageMap1.AddElipse("Changi Airport", 763, 369, 10);

            //North-South Line
            this.imageMap1.AddElipse("Bukit Batok", 166, 228, 10);
            this.imageMap1.AddElipse("Bukit Gombak", 166, 178, 10);
            this.imageMap1.AddElipse("Choa Chu Kang", 166, 126, 10);
            this.imageMap1.AddElipse("Yew Tee", 166, 75, 10);
            this.imageMap1.AddElipse("Kranji", 205, 42, 10);
            this.imageMap1.AddElipse("Marsiling", 251, 42, 10);
            this.imageMap1.AddElipse("Woodlands", 299, 42, 10);
            this.imageMap1.AddElipse("Admiralty", 347, 42, 10);
            this.imageMap1.AddElipse("Sembawang", 394, 42, 10);
            this.imageMap1.AddElipse("Yishun", 434, 68, 10);
            this.imageMap1.AddElipse("Khatib", 434, 100, 10);
            this.imageMap1.AddElipse("Yio Chu Kang", 434, 132, 10);
            this.imageMap1.AddElipse("Ang Mo Kio", 434, 164, 10);
            this.imageMap1.AddElipse("Bishan", 434, 200, 10);
            this.imageMap1.AddElipse("Braddell", 434, 226, 10);
            this.imageMap1.AddElipse("Toa Payoh", 420, 256, 10);
            this.imageMap1.AddElipse("Novena", 400, 276, 10);
            this.imageMap1.AddElipse("Newton", 380, 297, 10);
            this.imageMap1.AddElipse("Orchard", 360, 317, 10);
            this.imageMap1.AddElipse("Somerset", 366, 342, 10);
            this.imageMap1.AddElipse("Dhoby Ghaut", 396, 370, 10);
            this.imageMap1.AddElipse("Marina Bay", 422, 525, 10);

            //North-East Line
            this.imageMap1.AddElipse("HarbourFront", 285, 482, 10);
            this.imageMap1.AddElipse("Chinatown", 352, 416, 10);
            this.imageMap1.AddElipse("Clarke Quay", 373, 393, 10);
            this.imageMap1.AddElipse("Little India", 425, 342, 10);
            this.imageMap1.AddElipse("Farrer Park", 445, 322, 10);
            this.imageMap1.AddElipse("Boon Keng", 468, 299, 10);
            this.imageMap1.AddElipse("Potong Pasir", 485, 281, 10);
            this.imageMap1.AddElipse("Woodleigh", 507, 260, 10);
            this.imageMap1.AddElipse("Serangoon", 530, 236, 10);
            this.imageMap1.AddElipse("Kovan", 554, 213, 10);
            this.imageMap1.AddElipse("Hougang", 578, 190, 10);
            this.imageMap1.AddElipse("Buangkok", 601, 167, 10);
            this.imageMap1.AddElipse("Sengkang", 625, 143, 10);
            this.imageMap1.AddElipse("Punggol", 685, 82, 10);

            //Circle Line
            this.imageMap1.AddElipse("Telok Blangah", 258, 455, 10);
            this.imageMap1.AddElipse("Labrador Park", 239, 436, 10);
            this.imageMap1.AddElipse("Pasir Panjang", 219, 416, 10);
            this.imageMap1.AddElipse("Haw Par Villa", 203, 400, 10);
            this.imageMap1.AddElipse("Kent Ridge", 200, 383, 10);
            this.imageMap1.AddElipse("one-north", 217, 367, 10);
            this.imageMap1.AddElipse("Holland Village", 277, 306, 10);
            this.imageMap1.AddElipse("Farrer Road", 307, 276, 10);
            this.imageMap1.AddElipse("Botanic Gardens", 337, 245, 10);
            this.imageMap1.AddElipse("Caldecott", 367, 215, 10);
            this.imageMap1.AddElipse("Marymount", 403, 199, 10);
            this.imageMap1.AddElipse("Lorong Chuan", 478, 200, 10);
            this.imageMap1.AddElipse("Bartley", 553, 262, 10);
            this.imageMap1.AddElipse("Tai Seng", 563, 278, 10);
            this.imageMap1.AddElipse("MacPherson", 563, 298, 10);
            this.imageMap1.AddElipse("Dakota", 563, 366, 10);
            this.imageMap1.AddElipse("Mountbatten", 563, 385, 10);
            this.imageMap1.AddElipse("Stadium", 545, 403, 10);
            this.imageMap1.AddElipse("Nicoll Highway", 523, 425, 10);
            this.imageMap1.AddElipse("Promenade", 501, 447, 10);
            this.imageMap1.AddElipse("Esplanade", 469, 434, 10);
            this.imageMap1.AddElipse("Bras Basah", 425, 390, 10);

            this.imageMap1.AddElipse("Bayfront", 458, 490, 10);

        }

		/// Clean up any resources being used.
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form2));
            this.label3 = new System.Windows.Forms.Label();
            this.fromBox = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.toBox = new System.Windows.Forms.ComboBox();
            this.label4 = new System.Windows.Forms.Label();
            this.btnClear = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label6 = new System.Windows.Forms.Label();
            this.txtRoute = new System.Windows.Forms.TextBox();
            this.imageMap1 = new DotNetOpenSource.Controls.ImageMap();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(855, 291);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(39, 13);
            this.label3.TabIndex = 15;
            this.label3.Text = "Route:";
            // 
            // fromBox
            // 
            this.fromBox.FormattingEnabled = true;
            this.fromBox.Items.AddRange(new object[] {
            "Admiralty",
            "Aljunied",
            "Ang Mo Kio",
            "Bartley",
            "Bayfront",
            "Bedok",
            "Bishan",
            "Boon Keng",
            "Boon Lay",
            "Botanic Gardens",
            "Braddell",
            "Bras Basah",
            "Buangkok",
            "Bugis",
            "Bukit Batok",
            "Bukit Gombak",
            "Buona Vista",
            "Caldecott",
            "Changi Airport",
            "Chinatown",
            "Chinese Garden",
            "Choa Chu Kang",
            "City Hall",
            "Clarke Quay",
            "Clementi",
            "Commonwealth",
            "Dakota",
            "Dhoby Ghaut",
            "Dover",
            "Esplanade",
            "Eunos",
            "Expo",
            "Farrer Park",
            "Farrer Road",
            "HarbourFront",
            "Haw Par Villa",
            "Holland Village",
            "Hougang",
            "Joo Koon",
            "Jurong East",
            "Kallang",
            "Kembangan",
            "Kent Ridge",
            "Khatib",
            "Kovan",
            "Kranji",
            "Labrador Park",
            "Lakeside",
            "Lavender",
            "Little India",
            "Lorong Chuan",
            "MacPherson",
            "Marina Bay",
            "Marsiling",
            "Marymount",
            "Mountbatten",
            "Newton",
            "Nicoll Highway",
            "Novena",
            "one-north",
            "Orchard",
            "Outram Park",
            "Pasir Panjang",
            "Pasir Ris",
            "Paya Lebar",
            "Pioneer",
            "Potong Pasir",
            "Promenade",
            "Punggol",
            "Queenstown",
            "Raffles Place",
            "Redhill",
            "Sembawang",
            "Sengkang",
            "Serangoon",
            "Simei",
            "Somerset",
            "Stadium",
            "Tai Seng",
            "Tampines",
            "Tanah Merah",
            "Tanjong Pagar",
            "Telok Blangah",
            "Tiong Bahru",
            "Toa Payoh",
            "Woodlands",
            "Woodleigh",
            "Yew Tee",
            "Yio Chu Kang",
            "Yishun"});
            this.fromBox.Location = new System.Drawing.Point(900, 148);
            this.fromBox.Name = "fromBox";
            this.fromBox.Size = new System.Drawing.Size(217, 21);
            this.fromBox.TabIndex = 14;
            this.fromBox.Text = "Choose Mrt Station";
            this.fromBox.SelectedIndexChanged += new System.EventHandler(this.fromBox_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(815, 187);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(79, 13);
            this.label2.TabIndex = 12;
            this.label2.Text = "Ending Station:";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(826, 151);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(68, 13);
            this.label1.TabIndex = 11;
            this.label1.Text = "Start Station:";
            // 
            // toBox
            // 
            this.toBox.FormattingEnabled = true;
            this.toBox.Items.AddRange(new object[] {
            "Admiralty",
            "Aljunied",
            "Ang Mo Kio",
            "Bartley",
            "Bayfront",
            "Bedok",
            "Bishan",
            "Boon Keng",
            "Boon Lay",
            "Botanic Gardens",
            "Braddell",
            "Bras Basah",
            "Buangkok",
            "Bugis",
            "Bukit Batok",
            "Bukit Gombak",
            "Buona Vista",
            "Caldecott",
            "Changi Airport",
            "Chinatown",
            "Chinese Garden",
            "Choa Chu Kang",
            "City Hall",
            "Clarke Quay",
            "Clementi",
            "Commonwealth",
            "Dakota",
            "Dhoby Ghaut",
            "Dover",
            "Esplanade",
            "Eunos",
            "Expo",
            "Farrer Park",
            "Farrer Road",
            "HarbourFront",
            "Haw Par Villa",
            "Holland Village",
            "Hougang",
            "Joo Koon",
            "Jurong East",
            "Kallang",
            "Kembangan",
            "Kent Ridge",
            "Khatib",
            "Kovan",
            "Kranji",
            "Labrador Park",
            "Lakeside",
            "Lavender",
            "Little India",
            "Lorong Chuan",
            "MacPherson",
            "Marina Bay",
            "Marsiling",
            "Marymount",
            "Mountbatten",
            "Newton",
            "Nicoll Highway",
            "Novena",
            "one-north",
            "Orchard",
            "Outram Park",
            "Pasir Panjang",
            "Pasir Ris",
            "Paya Lebar",
            "Pioneer",
            "Potong Pasir",
            "Promenade",
            "Punggol",
            "Queenstown",
            "Raffles Place",
            "Redhill",
            "Sembawang",
            "Sengkang",
            "Serangoon",
            "Simei",
            "Somerset",
            "Stadium",
            "Tai Seng",
            "Tampines",
            "Tanah Merah",
            "Tanjong Pagar",
            "Telok Blangah",
            "Tiong Bahru",
            "Toa Payoh",
            "Woodlands",
            "Woodleigh",
            "Yew Tee",
            "Yio Chu Kang",
            "Yishun"});
            this.toBox.Location = new System.Drawing.Point(900, 184);
            this.toBox.Name = "toBox";
            this.toBox.Size = new System.Drawing.Size(217, 21);
            this.toBox.TabIndex = 10;
            this.toBox.Text = "Choose Mrt Station";
            this.toBox.SelectedIndexChanged += new System.EventHandler(this.toBox_SelectedIndexChanged);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(814, 253);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(80, 13);
            this.label4.TabIndex = 18;
            this.label4.Text = "Time Travelled:";
            // 
            // btnClear
            // 
            this.btnClear.Location = new System.Drawing.Point(1059, 220);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(58, 23);
            this.btnClear.TabIndex = 19;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(897, 253);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(10, 13);
            this.label5.TabIndex = 20;
            this.label5.Text = " ";
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(796, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(338, 96);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 13;
            this.pictureBox1.TabStop = false;
            // 
            // panel1
            // 
            this.panel1.AllowDrop = true;
            this.panel1.BackColor = System.Drawing.Color.Transparent;
            this.panel1.BackgroundImage = global::WindowsFormsApplication1.Properties.Resources.mrt_no_lrt;
            this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.panel1.ForeColor = System.Drawing.Color.Transparent;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(799, 557);
            this.panel1.TabIndex = 21;
            this.panel1.Visible = false;            
            // 
            // label6
            // 
            this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label6.ForeColor = System.Drawing.Color.Maroon;
            this.label6.Location = new System.Drawing.Point(834, 105);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(288, 40);
            this.label6.TabIndex = 22;
            this.label6.Text = "Note: You can also select your stations by clicking 2 station from the map";
            // 
            // txtRoute
            // 
            this.txtRoute.Location = new System.Drawing.Point(900, 291);
            this.txtRoute.Multiline = true;
            this.txtRoute.Name = "txtRoute";
            this.txtRoute.ReadOnly = true;
            this.txtRoute.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtRoute.Size = new System.Drawing.Size(217, 237);
            this.txtRoute.TabIndex = 16;
            this.txtRoute.Text = "Select a starting and ending station.";
            // 
            // imageMap1
            // 
            this.imageMap1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.imageMap1.Image = global::WindowsFormsApplication1.Properties.Resources.mrt_no_lrt;
            this.imageMap1.Location = new System.Drawing.Point(0, 0);
            this.imageMap1.Name = "imageMap1";
            this.imageMap1.Size = new System.Drawing.Size(799, 557);
            this.imageMap1.TabIndex = 0;
            this.imageMap1.RegionClick += new DotNetOpenSource.Controls.ImageMap.RegionClickDelegate(this.imageMap1_RegionClick);            
            // 
            // Form2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(1134, 558);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.txtRoute);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.fromBox);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.toBox);
            this.Controls.Add(this.imageMap1);
            this.Controls.Add(this.panel1);
            this.Name = "Form2";
            this.Text = "MRT Route Planner";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void imageMap1_RegionClick(int index, string key)
		{
            if (startStn == null)
            {
                startStn = key;
                this.fromBox.Text = key;
            }
            else if (startStn != null && endStn == null)
            {
                endStn = key;
                this.toBox.Text = key;
                displayRoute();
            }
		}

        private void btnClear_Click(object sender, EventArgs e)
        {
            startStn = null;
            endStn = null;
            this.fromBox.Text = "Choose Mrt Station";
            this.toBox.Text = "Choose Mrt Station";
            this.txtRoute.Text = "Choose a starting and ending station.";
            this.label5.Text = "";
            panel1.Visible = false; 
            panel1.Controls.Clear();
            this.pathIndex.Clear();              
        }

        private void fromBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            startStn = fromBox.SelectedItem.ToString();

            if (startStn != null && endStn != null)
                displayRoute();    
        }

        private void toBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            endStn = toBox.SelectedItem.ToString();
            
            if (startStn != null && endStn != null)
                displayRoute();    
        }

        private void displayRoute()
        {
            // execute the rule and get a term integer back
            int term = ls.ExecStr("find_distance(\"" + startStn + "\", \"" + endStn + "\", P, D).");

            // Get the first item in the Path route
            int routeTerm = ls.GetArg(term, 3);

            // String variable to store the paths
            string route = ls.GetStrHead(routeTerm);
            this.pathIndex.Add(this.imageMap1.getIndexByKey(route));
            routeTerm = ls.GetTail(routeTerm);
            // while there are still items in the Path list, loop and get it
            while (routeTerm != 0)
            {
                this.pathIndex.Add(this.imageMap1.getIndexByKey(ls.GetStrHead(routeTerm)));
                route = route + "\r\n" + ls.GetStrHead(routeTerm);
                routeTerm = ls.GetTail(routeTerm);
            }

            // set the textbox with the route
            this.txtRoute.Text = route;

            // displayed visited GUI
            foreach (int i in this.pathIndex)
            {   
                int x = this.imageMap1.getX(i)-5;
                int y = this.imageMap1.getY(i)-5;
                addVisited(x,y);
            }

            // set the label with the time needed
            this.label5.Text = ls.GetIntArg(term, 4).ToString() + " min";
        }

        public void addVisited(int x, int y)
        {
            PictureBox picLit = new PictureBox();
            picLit.Image = global::WindowsFormsApplication1.Properties.Resources.red_circle;
            picLit.SizeMode = PictureBoxSizeMode.CenterImage;
            picLit.Location = new Point(x,y);
            picLit.Width = 10;
            picLit.Height = 10;            
            panel1.Visible = true;
            panel1.Controls.Add(picLit);            
            this.imageMap1.SendToBack();
       }
	}
}
